package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.Users;
import com.jr.service.IUserService;
import com.jr.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/user/*")
public class UserController extends HttpServlet {

    IUserService ius=new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI(); // 获取完整请求路径  --- /aaa/projectInfo/showdt
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index); // 截取最后一个"/"之后的字符串作为方法名

        System.out.println("requestURI: " + requestURI);
        System.out.println("截取的方法名: " + mName); // 应输出 "finduser"
        System.out.println("*********** UserController line30 *******************");
        /* 反射获取并调用对应方法 */
        Class cla = this.getClass(); // 获取当前类的Class对象
        Method method = null;
        try {
            // 查找当前类中是否存在指定名称、且参数为HttpServletRequest和HttpServletResponse的方法
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);

            response.setContentType("application/json;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");

            // 调用找到的方法，this表示当前类实例，传递request和response作为参数
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace(); // 找不到对应方法的处理（这会导致404错误）
        } catch (IllegalAccessException e) {
            e.printStackTrace(); // 方法访问权限不足的处理
        } catch (InvocationTargetException e) {
            e.printStackTrace(); // 方法调用过程中抛出异常的处理
        }
    }

    //根据 在index.jsp 输入的 账号 密码 找有没有
    public void finduser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userCode = request.getParameter("userCode");
        String pwd = request.getParameter("pwd");

        boolean boo=false;
        String password = ius.queryPwdByUserCode(userCode);
        if(pwd.equals(password)){
            boo=true;
        }
        response.getWriter().println(new Gson().toJson(boo));

    }




}
